#!/bin/bash
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 11 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## p_platform_score
##
## Slackware version
##
## Determines the "platform score" for the running system. This score is a
## value from 0 (no similarity) to 100 (definite match), with a minimum score
## of 60 required for the platform to be a possible match. This function
## examines the system to determine platform similarity. Returns 0 and echoes
## the generic platform name and score in name=score format.
##
function p_platform_score() {
	local score=0
	local check=
	
	if [[ -f "${P_ROOT}/etc/slackware-version" ]]; then
		score=$((score + 20))
	fi
	
	if [[ -x "${P_ROOT}/sbin/installpkg" ]]; then
		score=$((score + 20))
	fi
	
	if [[ -f "${P_ROOT}/etc/os-release" ]]; then
		check=$(cat "${P_ROOT}/etc/os-release" | grep ^ID= | sed s/^ID+//)
		
		if [[ "${check}" == "slackware" ]]; then
			score=$((score + 10))
		fi
	fi
	
	if [[ -f "${P_ROOT}/etc/rc.d/rc.S" ]]; then
		score=$((score + 10))
	fi
	
	if [[ -d "${P_ROOT}/var/log/packages" ]]; then
		score=$((score + 20))
		
		# Look to see if one or more kernel packages is installed
		ls "${P_ROOT}/var/log/packages/kernel*" > /dev/null 2>&1
		if [[ $? -eq 0 ]]; then
			score=$((score + 20))
		fi
	fi
	
	echo "slackware=${score}"
	return 0
}


## p_platform_set_context
##
## Slackware version
##
## Writes the platform generic name to the platform/name context item, the
## actual distribution name to platform/os/name, the kernel architecture to
## platform/kernel/arch, and the system package architecture to
## platform/os/arch. Returns 0 if context information is written successfully,
## non-zero otherwise.
##
function p_platform_set_context() {
	local status=0
	local arch=
	
	set_context platform/name "slackware"
	set_context platform/os/name "$(cat /etc/slackware-version)"
	status=$?
	
	arch=$(uname -m)
	status=$(merge_status ${status} $?)
	
	set_context platform/kernel/arch "${arch}"
	status=$(merge_status ${status} $?)
	
	if [[ "${arch}" == "x86_64" ]]; then
		set_context platform/os/arch x86_64
		status=$(merge_status ${status} $?)
	else
		set_context platform/os/arch i486
		status=$(merge_status ${status} $?)
	fi
	
	return ${status}
}
